﻿using HomeCareLibrary.AppService;
using HomeCareLibrary.Constants;
using HomeCareLibrary.Core.DataContracts;
using HomeCareLibrary.Core.Dtos;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HomeCareLibrary.Controllers
{
    public class CustomTasksController : BaseController
    {
        //
        // GET: /CustomTasks/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Add(string homeNumber)
        {
            //Session[SessionConstants.MODE] = ApplicationModeConstants.IPHONE_APP; //Only for Testing
            
            if (Session[SessionConstants.USER_ID] != null)
            {
                Int32 userId = Convert.ToInt32(Session[SessionConstants.USER_ID].ToString());
                UserAppService objUserAppService = new UserAppService();
                if (homeNumber == null)
                {
                    if (Session[SessionConstants.HOME_NUMBER] == null || Convert.ToInt32(Session[SessionConstants.HOME_NUMBER]) == 0)
                    {
                        var getHomeNumber = objUserAppService.GetHomeNumber(userId);
                        homeNumber = getHomeNumber.ToString();
                        Session[SessionConstants.HOME_NUMBER] = homeNumber;
                    }
                    else
                    {
                        homeNumber = Session[SessionConstants.HOME_NUMBER].ToString();
                    }

                }
                else
                {

                    Session[SessionConstants.TASK_MONTH] = Convert.ToString(homeNumber); //assign value to session
                }
                //Session[SessionConstants.MODE] = ApplicationModeConstants.ANDROID_APP; //For Testing
                TasksAppService objTasksAppService = new TasksAppService();

                CRCTaskRequest objrequest = new CRCTaskRequest();
                objrequest.UserId=userId;
                objrequest.HomeNumber=Convert.ToInt32(homeNumber);
                var GetCustomList=  objTasksAppService.GetCustomTasksList(objrequest);


                ViewBag.sessionIds = "Session Id: " + Session.SessionID.ToString() + ", " + SessionConstants.USER_ID + "=" + Session[SessionConstants.USER_ID] + ", " + SessionConstants.USER_NAME + "=" + Session[SessionConstants.USER_NAME] + ", " + SessionConstants.PASSWORD + "=" + Session[SessionConstants.PASSWORD] + ", " + SessionConstants.MODE + "=" + Session[SessionConstants.MODE] + ", " + SessionConstants.ACTIVITY_TYPE + "=" + Session[SessionConstants.ACTIVITY_TYPE] + ", " + SessionConstants.PROMO_CODE + "=" + Session[SessionConstants.PROMO_CODE] + ", " + SessionConstants.SPONSOR_DETAILS + "=" + Session[SessionConstants.SPONSOR_DETAILS];

                string str = null;
                foreach (string key in Session.Keys)
                {
                    try
                    {
                        if (Session[key] != null)
                        {

                            str += string.Format("{0}: {1}<br />", key, Session[key].ToString());
                        }
                    }
                    catch
                    { }
                }
                ViewBag.getSession = str;

                Request.InputStream.Position = 0;
                StreamReader postedVariableStream = new StreamReader(Request.InputStream);
                string postedVariableJsonString = postedVariableStream.ReadToEnd();
                ViewBag.JsonString = postedVariableJsonString;

                string getContentType = Request.ContentType;
                ViewBag.ContentType = getContentType;

                string getRequestUrl = Request.Url.ToString();
                ViewBag.Url = getRequestUrl;
                PersonalisationAppService objPersonalisationAppService = new PersonalisationAppService();
                GetUserHomeRequest objGetUserHomeRequest = new GetUserHomeRequest();
                objGetUserHomeRequest.UserId = userId;
                var getUserHome1 = objPersonalisationAppService.GetUserHomes(objGetUserHomeRequest);
                if (getUserHome1.Count == 1)
                {
                    ViewBag.getSingleHomeName = getUserHome1.FirstOrDefault().home_name;
                    ViewBag.getSingleHomeValue = getUserHome1.FirstOrDefault().home_number;
                }
                else
                {
                    ViewBag.getSingleHomeName = "";
                    ViewBag.getSingleHomeValue = "";
                    ViewBag.getHomeName = new SelectList(getUserHome1, "home_number", "home_name", homeNumber);
                }
                return View(GetCustomList);
            }
            else
            {
                return RedirectToAction("Login", "Home");
            }
        }

         public ActionResult Delete()
        {
            ViewBag.sessionIds = "Session Id: " + Session.SessionID.ToString() + ", " + SessionConstants.USER_ID + "=" + Session[SessionConstants.USER_ID] + ", " + SessionConstants.USER_NAME + "=" + Session[SessionConstants.USER_NAME] + ", " + SessionConstants.PASSWORD + "=" + Session[SessionConstants.PASSWORD] + ", " + SessionConstants.MODE + "=" + Session[SessionConstants.MODE] + ", " + SessionConstants.ACTIVITY_TYPE + "=" + Session[SessionConstants.ACTIVITY_TYPE] + ", " + SessionConstants.PROMO_CODE + "=" + Session[SessionConstants.PROMO_CODE] + ", " + SessionConstants.SPONSOR_DETAILS + "=" + Session[SessionConstants.SPONSOR_DETAILS];

            string str = null;
            foreach (string key in Session.Keys)
            {
                try
                {
                    if (Session[key] != null)
                    {

                        str += string.Format("{0}: {1}<br />", key, Session[key].ToString());
                    }
                }
                catch
                { }
            }
            ViewBag.getSession = str;
             
             
             
             Request.InputStream.Position = 0;
            StreamReader postedVariableStream = new StreamReader(Request.InputStream);
            string postedVariableJsonString = postedVariableStream.ReadToEnd();
            ViewBag.JsonString = postedVariableJsonString;

            string getContentType = Request.ContentType;
            ViewBag.ContentType = getContentType;

            string getRequestUrl = Request.Url.ToString();
            ViewBag.Url = getRequestUrl;

            return View();
        }

         public ActionResult TaskDetail(string taskCode)
         {
             //Session[SessionConstants.USER_ID] = null;
             //Session[SessionConstants.MODE] = ApplicationModeConstants.ANDROID_APP; //Only for Testing
             TasksAppService service = new TasksAppService();
             try
             {
                 if (Session[SessionConstants.USER_ID] != null)
                 {
                     Int32 userId = Convert.ToInt32(Session[SessionConstants.USER_ID].ToString());
                     Int32 homeNumber = 1;

                     UserAppService objUserAppService = new UserAppService();
                     if (Session[SessionConstants.HOME_NUMBER] == null || Convert.ToInt32(Session[SessionConstants.HOME_NUMBER]) == 0)
                     {
                         var getHomeNumber = objUserAppService.GetHomeNumber(userId);
                         homeNumber = getHomeNumber;
                         Session[SessionConstants.HOME_NUMBER] = homeNumber;
                     }
                     else
                     {
                         homeNumber = Convert.ToInt32(Session[SessionConstants.HOME_NUMBER].ToString());
                     }


                     //Call Task App Service (GetTaskDetail)
                     TaskDetailRequest objTaskDetailRequest = new TaskDetailRequest();
                     objTaskDetailRequest.UserId = userId;
                     objTaskDetailRequest.HomeNumber = Convert.ToByte(homeNumber);
                     objTaskDetailRequest.TaskCode = taskCode.Trim();


                     //objTaskDetailRequest.UserId = 13577;
                     //objTaskDetailRequest.HomeNumber = Convert.ToByte(2);
                     //objTaskDetailRequest.TaskCode = "crc_21";

                     var taskDetail = service.GetTaskDetail(objTaskDetailRequest);
                     ViewBag.noRecords = "";
                     ViewBag.taskCodeStatus = taskCode.Trim();
                     //ViewBag.taskCodeStatus = "crc_21";
                     var taskDetailResult = (CTTaskDetailDto)taskDetail["TaskDetail"];


                     if (taskDetail["TaskDetail"] == null)
                     {
                         ViewBag.noRecords = "You have no Task Detail for this task based on your personalization.";
                     }

                     return PartialView("TaskDetail", taskDetailResult);
                 }
                 else
                 {
                     //In Js function (mtt.js) we redirect blank content to Login Page.
                     return Json("", JsonRequestBehavior.AllowGet);
                 }
             }
             catch
             {
                 //return RedirectToAction("Login", "Home");
                 return Json("", JsonRequestBehavior.AllowGet);
             }

         }

         public ActionResult DeleteTask(string id)
         {
             TasksAppService service = new TasksAppService();
             if (Session[SessionConstants.USER_ID] != null)
             {
                 Int32 userId = Convert.ToInt32(Session[SessionConstants.USER_ID].ToString());
                 Int32 homeNumber = 0;

                 UserAppService objUserAppService = new UserAppService();
                 if (Session[SessionConstants.HOME_NUMBER] == null || Convert.ToInt32(Session[SessionConstants.HOME_NUMBER]) == 0)
                 {
                     var getHomeNumber = objUserAppService.GetHomeNumber(userId);
                     homeNumber = getHomeNumber;
                     Session[SessionConstants.HOME_NUMBER] = homeNumber;
                 }
                 else
                 {
                     homeNumber = Convert.ToInt32(Session[SessionConstants.HOME_NUMBER].ToString());
                 }
                 TurnOffTaskRequest objTurnOffTaskRequest = new TurnOffTaskRequest();
                 objTurnOffTaskRequest.UserId = userId;
                 objTurnOffTaskRequest.HomeNumber = Convert.ToByte(homeNumber);
                 objTurnOffTaskRequest.TaskCode = id;
                 string getResultTurnOffTask = service.TurnOffTask(objTurnOffTaskRequest);

                 return RedirectToAction("Add");

             }
             else
             {

                 return RedirectToAction("Login", "Home");
             }

         }
    }
}
